/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package chenxu.test.jms.storm;

import javax.jms.Session;

import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.contrib.jms.JmsProvider;
import backtype.storm.contrib.jms.JmsTupleProducer;
import backtype.storm.contrib.jms.spout.JmsSpout;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.tuple.Fields;
import backtype.storm.utils.Utils;
import chenxu.test.jms.SpringJmsProvider;


public class BcpJmsTopology {
	public static final String JMS_QUEUE_SPOUT = "JMS_QUEUE_SPOUT";
	public static final String SPLIT_BCP = "SPLIT_BCP";
	public static final String INSERT_HBASE = "INSERT_HBASE";
	public static final String JMS_TOPIC_BOLT = "JMS_TOPIC_BOLT";
	public static final String JMS_TOPIC_SPOUT = "JMS_TOPIC_SPOUT";
	public static final String ANOTHER_BOLT = "ANOTHER_BOLT";

	@SuppressWarnings("serial")
	public static void main(String[] args) throws Exception {

		// JMS Queue Provider
		JmsProvider jmsQueueProvider = new SpringJmsProvider(
				"jms-activemq.xml", "jmsConnectionFactory",
				"notificationQueue");
		
		// JMS Producer
		JmsTupleProducer producer = new BlobTupleProducer();

		// JMS Queue Spout
		JmsSpout queueSpout = new JmsSpout();
		queueSpout.setJmsProvider(jmsQueueProvider);
		queueSpout.setJmsTupleProducer(producer);
		queueSpout.setJmsAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
		queueSpout.setDistributed(true); // allow multiple instances

		TopologyBuilder builder = new TopologyBuilder();
		
		// spout with 5 parallel instances
		builder.setSpout(JMS_QUEUE_SPOUT, queueSpout, 1);

		// intermediate bolt, subscribes to jms spout, anchors on tuples, and auto-acks
		String[] fields = new String[62];
		for(int i=0; i<62; i++) {
			fields[i] = "field"+i;
		}
		Fields declaredFields = new Fields(fields);
		
		builder.setBolt(SPLIT_BCP,
				new BcpSplitBolt(SPLIT_BCP, true, true, declaredFields), 1)
//				.allGrouping(JMS_QUEUE_SPOUT);
				.shuffleGrouping(JMS_QUEUE_SPOUT);
//				.noneGrouping(JMS_QUEUE_SPOUT);
//				.directGrouping(JMS_QUEUE_SPOUT);


		// bolt that subscribes to the intermediate bolt, and auto-acks
		// messages.
		builder.setBolt(INSERT_HBASE, new GenericHbaseBolt(INSERT_HBASE, true, true), 1)
		.shuffleGrouping(SPLIT_BCP);
//		.allGrouping(SPLIT_BCP);
//		.noneGrouping(INTERMEDIATE_BOLT);
		
		

		Config conf = new Config();

		conf.setDebug(true);
		if (args.length > 0) {
			conf.setNumWorkers(3);

			StormSubmitter.submitTopology(args[0], conf,
					builder.createTopology());
		} else {

			conf.setDebug(false);

			LocalCluster cluster = new LocalCluster();
			cluster.submitTopology("storm-jms-example", conf, builder.createTopology());
			Utils.sleep(600000000);
			cluster.killTopology("storm-jms-example");
			cluster.shutdown();
		}
	}

}
